package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;


	@Autowired
	private VodService vodService;





	/**
	 * 删除声音信息
	 * api/album/trackInfo/removeTrackInfo/{id}
	 * @param id
	 * @return
	 */
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){

		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}


	/**
	 * 修改保存声音
	 * api/album/trackInfo/updateTrackInfo/{id}
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,
								  @RequestBody TrackInfoVo trackInfoVo){

		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}


	/**
	 * 修改声音回显
	 * api/album/trackInfo/getTrackInfo/{id}
	 * @param id
	 * @return
	 */
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id ){

		return Result.ok(trackInfoService.getById(id));
	}



	/**
	 * 获取当前用户声音分页列表
	 * api/album/trackInfo/findUserTrackPage/{page}/{limit}
	 * @param query
	 * @param page
	 * @param limit
	 * @return
	 */
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<TrackListVo>> findUserTrackPage(@RequestBody TrackInfoQuery query,
													   @PathVariable Long page,
													   @PathVariable Long limit ){

		//封装条件数据
		Long userId = AuthContextHolder.getUserId();
		query.setUserId(userId);
		//封装分页对象
		Page<TrackListVo> trackListVoPage = new Page<>(page,limit);
		//调用sesrvice
		trackListVoPage=trackInfoService.findUserTrackPage(trackListVoPage,query);

		return Result.ok(trackListVoPage);
	}


	/**
	 * 保存声音
	 * api/album/trackInfo/saveTrackInfo
	 * @param trackInfoVo
	 * @return
	 */
	@PostMapping("/trackInfo/saveTrackInfo")
	@GuiGuLogin
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){

		trackInfoService.saveTrackInfo(trackInfoVo);

		return Result.ok();
	}




	/**
	 * 上传声音
	 * api/album/trackInfo/uploadTrack
	 * @param file
	 * @return
	 */
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String,String>> uploadTrack(MultipartFile file){
		Map<String,String> fileMap=	vodService.uploadTrack(file);
		return Result.ok(fileMap);
	}

	@GuiGuLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@PathVariable Long albumId,
			@PathVariable Long page,
			@PathVariable Long limit
	) {
		IPage trackPage = new Page(page,limit);

		 trackPage = trackInfoService.findAlbumTrackPage(trackPage,albumId);

		return Result.ok(trackPage);

	}

}

